package redis

import "time"

// 缓存短信验证码
func RedisSetSmsCode(mobile, code, sale string) {
	key := "sms:" + mobile + sale
	RedisDB.Set(key, code, time.Minute*10)
}

// 获取短信验证码
func RedisGetSmsCode(mobile, sale string) string {
	key := "sms:" + mobile + sale
	return RedisDB.Get(key).Val()
}

// 短信限流  一分钟发送一次
func SmsCodeLimit(mobile string) bool {
	key := "smsLimit:" + mobile
	res := RedisDB.Get(key).Val()

	if res == "" {
		RedisDB.Set(key, "0", time.Minute*1)
	}
	if res < "1" {
		RedisDB.Incr(key)
		return true
	}
	return false
}

// 销毁短信验证码
func RedisDelSmsCode(mobile, sale string) {
	key := "sms:" + mobile + sale
	RedisDB.Del(key)
}
